
import {
    login
} from '@/comApi'
import VueCookies from 'vue-cookies'

const Login = {
    state: {
        isLogin: false,
        userInfo: {}
    },
    mutations: {
        CommitUserInfo(state, userInfo) {
            state.userInfo = userInfo
            if (userInfo.token) {
                state.isLogin = true
                VueCookies.set('Authorization',userInfo.token,86400)
                VueCookies.set('isLogin',state.isLogin,86400)
            }
        }
    },
    actions: {
        asyncLogin({commit}, params) {
        return login(params)
            .then(data => {
                commit('CommitUserInfo', data)
                return Promise.resolve(data)
            })
            .catch(error => {
                return Promise.reject(error)
            })
        }
    },
    modules: {}
}

export default Login